package com.cqeec.netdisc0215.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqeec.netdisc0215.service.UserService;

@Controller
@RequestMapping("/user/")
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/change/password")
	@ResponseBody
	public Map<String, Object> changePassword(String id, String oldPassword, String newPassword,
			String confirmPassword) {
		Map<String, Object> result = new HashMap<>();
		// 验证用户原密码正确
		if (userService.checkUserPassword(id, oldPassword)) {
			// 检测两次新密码是否一致
			if (newPassword.equals(confirmPassword)) {
				// 修改用户密码
				try {
					userService.updatePassword(id, newPassword);
					result.put("code", 0);
					result.put("message", "密码修改成功！");
				} catch (Exception e) {
					result.put("code", 3);
					result.put("message", "密码修改失败！");
				}
			} else {
				result.put("code", 2);
				result.put("message", "新密码与确认密码不一致！");
			}
		} else {
			result.put("code", 1);
			result.put("message", "用户原密码错误！");
		}
		return result;
	}
}
